<template>
  <div class="app-container">
    <el-form :model="formData" label-width="120px" label-position="left">
      <el-form-item label="所属高中">
          <el-select filterable v-model="formData.highSchoolId" placeholder="请选择高中">
            <el-option
              v-for="item in highSchoolList"
              :key="item.highSchoolId"
              :label="item.highSchoolName"
              :value="item.highSchoolId">
            </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="班级名称">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
    </el-form>
    <div class="submit-box">
      <el-button type="primary" :loading="loading" style="width: 200px;" @click.native.prevent="doSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
  import { create } from '@/api/high_school_class'
  import { getAll } from '@/api/high_school'
  import { errorMsg, successMsg } from '@/utils/my'

  export default {
    data() {
      return {
        loading: false,
        formData: {
          name: '',
          highSchoolId: ''
        },
        highSchoolList: []
      }
    },

    created() {
      this.fetchHighSchoolList()
    },

    methods: {

      doSubmit() {
        let that = this,
            d = JSON.parse(JSON.stringify(that.formData))
        if (d.highSchoolId.length <= 0) {
          return errorMsg('请选择高中')
        }
        if (d.name.length <= 0) {
          return errorMsg('请输入班级名称')
        }
        d.highSchoolId = d.highSchoolId.length <= 0 ? 0 : d.highSchoolId
        that.loading = true
        create(d).then(res => {
          that.loading = false
          if (res.code != 20000) {
            return errorMsg(res.message)
          }
          successMsg(res.message)
          setTimeout(function() {
            that.$router.push({path: '/admin/high-school-class/list'})
          }, 1500)
        })
      },

      fetchHighSchoolList() {
        const that = this
        getAll().then(res => {
          that.highSchoolList = res.data
        })
      }

    }
  }
</script>

<style>
  .submit-box {
    height: 50px;
    line-height: 50px;
    text-align: center;
    padding-right: 20px;
  }
</style>
